<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/20
 * Time: 17:02
 */

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\facade\Cache;
use think\facade\Cookie;
use think\facade\Session;
use think\Request;

class Article extends Common
{
    /**
     * 文章页面遍历
     * @return mixed
     */
    public function article()
    {
        $userInfo = Session::get("userClass" . Cookie::get("userId"));
        $this->assign("userInfo",$userInfo);
        return $this->fetch();
    }

    /**
     * 增加页面遍历
     * @return mixed
     */
    public function add(Request $request)
    {
        if ($this->request->isPost()) {
            $data = $this->deal_data($request->param());
            $article = new \app\index\model\Article();
            $result = $article->allowField(true)->save($data);
            if ($result){
                alter_redis();
                return json(["code" => 1 , "message" => "增加成功"]);
            } else {
                return json(["code" => 404 , "message" => "增加失败"]);
            }
        }
        return $this->fetch();
    }

    /**
     * 查询数据
     * @param Request $request
     * @return \think\response\Json
     */
    public function queryArticle(Request $request)
    {
        $page   = $request->param('page/d');
        $limit  = $request->param('limit/d');

        //权限不同给文章遍历
        $data =  new \app\index\model\Article();

        $data["data"] = $data->whereNotIn("status",0)->page($page)->limit($limit)->select();

        $data["count"] = $data->whereNotIn("status",0)->count();

        $data["code"] = 0;

        return json($data);
    }

    /**
     * 图片处理
     */
    public function uploadImage()
    {
        $file = $this->request->file('file');
        $info = $file->getInfo();

        if ($info["error"] > 0){
            echo json(["code"=> 0,"msg" => "图片上传错误"]);
        }

        if ($info["size"] > (24*1024*1024)) {
            echo json(["code" => 0, "msg" => "图片大小不能超过2Mb"]);
        }

        $filePath = "D:/down/PHPstudy/phpstudy_pro/WWW/APP/app_index/images/upload";

        $url = "http://www.wy.com/APP/app_index/images/upload/";
        $result = $file->move($filePath);

        $name = $result->getFilename();

        if (!$result) {
            return json(["code"=> 0,"msg" => "图片上传失败"]);
        }

        return json(["code"=> 1,"msg" => "图片已上传","data"=>$url .$name]);
    }

    /**
     * 删除操作
     * @param Request $request
     * @return \think\response\Json
     * @throws \Exception
     */
    public function deleteArticle(Request $request)
    {
        if ($this->request->isGet()) {
            $data = $request->param();
            $id = intval($data["id"]);
            $article = new \app\index\model\Article();
            $result = $article->where("id",$id)->delete();
            if ($result) {
                alter_redis();
                return json(["code" => 1 , "message" => "删除成功"]);
            }
            return json(["code" => 404 , "message" => "删除失败"]);
        }
    }

    /**
     * 修改操作
     * @param Request $request
     * @return mixed
     */
    public function edit(Request $request)
    {
        if ($this->request->isPost()) {
            $data = $this->deal_data($request->param());
            $article = new \app\index\model\Article();
            $result = $article->allowField(true)->save($data,["id" => $data["id"]]);
            if ($result){
                alter_redis();
                return json(["code" => 1 , "message" => "提交成功"]);
            } else {
                return json(["code" => 404 , "message" => "提交失败"]);
            }
        }
        $id = intval($request->param()["id"]);
        $data = \app\index\model\Article::where("id", $id)->find();
        $this->assign("data", $data);
        return $this->fetch();
    }

    /**
     * 处理数据
     * @param $data
     * @return mixed
     */
    public function deal_data($data){
        $date = date("F d Y",time());
        $data["date"] = $date;

        $time = time();
        if ($time > (24*60*60)) {
            $rows["time"] =  get_data($time);
        } else {
            $rows["time"] =  get_time($time);
        }
        $data["dateTime"] = $rows["time"];

        $data['detail'] = htmlspecialchars_decode($data['detail']);
        $data['detail'] = str_replace("&̲nbsp;","",$data['detail']);
        $data['detail'] = strip_tags($data['detail']);

        $data["content"] = substr($data["detail"],0,36);

        return $data;
    }
}

function get_time($value) {
    if ($value > 3600) {
        return ceil($value/3600) . "小时前";
    } else {
        return ceil($value/60) . "分钟前";
    }
}

function get_data($value) {
    $num =  ceil(intval($value)/(24*60*60));
    if ($num >= 3) {
        $num = 3;
    }
    return $num ."天前";
}

function alter_redis() {
    $mark = unserialize(Cache::get("article_list"));
    Cache::set("article_list",$mark,1);
    $banner = unserialize(Cache::get("banner"));
    Cache::set("banner",serialize($banner),1);
}